package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import net.wanho.dto.StoreDTO;
import net.wanho.po.Store;
import net.wanho.service.StoreService;
import net.wanho.vo.AjaxResult;
import net.wanho.vo.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RequiredArgsConstructor
@RestController
public class StoreController {

    private final StoreService storeService;

    @GetMapping("/store/store-list")
    public AjaxResult storeList() {
        return AjaxResult.success(storeService.list());
    }

    @GetMapping("/store/store-page-list")
    public AjaxResult storePageList(@RequestParam(defaultValue = "1") int pageNum,
                                    @RequestParam(defaultValue = "5") int pageSize,
                                    StoreDTO storeDTO) {
        PageInfo<Store> pageInfo = storeService.rolePageList(pageNum, pageSize, storeDTO);
        return AjaxResult.success("success",pageInfo);
    }

    @GetMapping("/store/store-num-check")
    public AjaxResult storeNumCheck(@RequestParam String storeNum) {
        Boolean storeNumCheck = storeService.storeNumCheck(storeNum);
        return AjaxResult.success("success", storeNumCheck);
    }

    @PostMapping("/api/store/store-add")
    public AjaxResult storeAdd(@RequestBody Store store) {
        storeService.save(store);
        return AjaxResult.success("仓库添加成功");
    }

    @PutMapping("/store/store-update")
    public AjaxResult storeUpdate(@RequestBody Store store) {
        storeService.updateById(store);
        return AjaxResult.success("仓库修改成功");
    }

    @DeleteMapping("/store/store-delete/{id}")
    public AjaxResult storeDelete(@PathVariable Integer id) {
        storeService.removeById(id);
        return AjaxResult.success("仓库删除成功");
    }

    @SneakyThrows
    @GetMapping("/store/exportTable")
    public AjaxResult exportTable(@RequestParam(defaultValue = "1") int pageNum,
                                  @RequestParam(defaultValue = "5") int pageSize,
                                  StoreDTO storeDTO) {
        PageInfo<Store> pageInfo = storeService.rolePageList(pageNum, pageSize, storeDTO);
        return AjaxResult.success("success", pageInfo.getResultList());
    }
}
